package thread;

/**
 * 有效的缩小同步范围可以在保证并发安全的前提下提高并发效率
 * 同步块的应用:
 * synchronized(同步监视器对象){
 *     需要多个线程同步执行的代码片段
 * }
 */
import com.sun.corba.se.impl.oa.poa.AOMEntry;

public class SyncApiDemo {
    public static void main(String[] args) {
        //StringBuilder sb = new StringBuilder();
        StringBuffer sb = new StringBuffer();

        Thread t1 = new Thread(){
            public void run(){
                for (int i=0;i<10000;i++){
                    sb.append("abc");
                }
            }
        };

        Thread t2 = new Thread(){
          public void run(){
              for(int i=0;i<10000;i++){
                  sb.append("def");
              }
          }
        };

        t1.start();
        t2.start();

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
        }
        System.out.println(sb.length());
    }
}
